package com.boyunv.composite.example02;
/*
 *@description
 *   容器对象,表示文件夹
 *@author boyunv
 *@create 2023/8/1 7:24
 *@version 1.0
 */

import java.util.ArrayList;

public class Directory extends Entry{
 private String name;

 private ArrayList<Entry>  directory=new ArrayList<>();

 public Directory(String name, ArrayList<Entry> directory) {
  this.name = name;
  this.directory = directory;
 }

 public Directory(String name) {
  this.name = name;
 }

 @Override
 public String getName() {
  return this.name;
 }

 /**
  * 获取文件大小
  *   1.如果entry是file类型,则调用getSize方法 获取文件大小
  *   2.如果entry对象是directory类型,会继续调用子文件的个体size()方法,形成递归调用
  * @return
  */
 @Override
 public int getSize() {
  int  size=0;
  for (Entry entry : directory) {
   size+=entry.getSize();
  }
  return size;
 }

 @Override
 public Entry add(Entry entry) {
  directory.add(entry);
  return this;
 }

 @Override
 public void printList(String prefix) {
  System.out.println(prefix+"/"+this);
  for (Entry entry : directory) {
   entry.printList(prefix+"/"+name);
  }
 }
}
